// ignore_for_file: deprecated_member_use

import 'package:flutter/material.dart';
import 'package:flutter_widget/custom/custom_appbar.dart';

// ignore: must_be_immutable
class LifeCycle extends StatelessWidget {
  String name;
  LifeCycle({Key? key, required this.name}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CustomAppbar(name, Theme.of(context).primaryColor),
      body: Center(
        child: Column(
          children: [
            StatelessWidgetPage(
              title: 'StatelessWidget',
            ),
            StatefulWidgetPage(
              title: 'StatefulWidget',
            ),
          ],
        ),
      ),
    );
  }
}

class StatelessWidgetPage extends StatelessWidget {
  final String title;
  StatelessWidgetPage({Key? key, required this.title}) : super(key: key) {
    print('StatelessWidget 构造函数被调用了!');
  }
  @override
  Widget build(BuildContext context) {
    print('StatelessWidget build方法被调用了!');
    return Center(
      child: Text(title),
    );
  }
}

class StatefulWidgetPage extends StatefulWidget {
  final String title;
  StatefulWidgetPage({Key? key, required this.title}) : super(key: key) {
    print('StatefulWidget 构造函数被调用了!');
  }
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<StatefulWidgetPage> {
  int _count = 0;

  _MyHomePageState() {
    print('StatefulWidget State构造方法来了!');
  }

  @override
  void initState() {
    print('StatefulWidget State的init来了!');
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    print('StatefulWidget State的build来了!');
    return Column(
      children: <Widget>[
        ElevatedButton(
          child: const Icon(Icons.add),
          onPressed: () {
            _count++;
            setState(() {});
          },
        ),
        Text('$_count'),
      ],
    );
  }

  @override
  void didChangeDependencies() {
    print('StatefulWidget didChangeDependencies');
    super.didChangeDependencies();
  }

  //当State对象从渲染树中移出的时候,就会调用!即将销毁!
  @override
  void deactivate() {
    super.deactivate();
  }

  @override
  void dispose() {
    print('StatefulWidget State的dispose');
    super.dispose();
  }
}
